package com.hy.admin.service.impl;

import com.hy.admin.dao.CourseSeriesDao;
import com.hy.admin.entity.CourseSeriesEntity;
import com.hy.admin.enums.EnumStatus;
import com.hy.admin.service.CourseSeriesService;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseSeriesServiceImpl implements CourseSeriesService {

    @Autowired
    private CourseSeriesDao courseSeriesDao;

    @Override
    public ServicesResult<Boolean> add(Long memberId, String name) {
        ServicesResult<Boolean> servicesResult = new ServicesResult<>();
        if (memberId == null || memberId <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        if (StringUtils.isBlank(name)){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        CourseSeriesEntity courseSeriesEntity = new CourseSeriesEntity();
        courseSeriesEntity.setMemberId(memberId);
        courseSeriesEntity.setName(name);
        courseSeriesEntity.setStatus(EnumStatus.VALID.getId());
        courseSeriesEntity.setCreateTime(new Date());
        int count = courseSeriesDao.insertSelective(courseSeriesEntity);
        if (count <= 0 ){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("添加失败");
            return servicesResult;
        }
        servicesResult.setResult(true);
        return servicesResult;
    }

    @Override
    public ServicesResult<Boolean> update(Long id, String name) {
        ServicesResult<Boolean> servicesResult = new ServicesResult<>();
        if (id == null || id <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        if (StringUtils.isBlank(name)){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        CourseSeriesEntity courseSeriesEntity = new CourseSeriesEntity();
        courseSeriesEntity.setId(id);
        courseSeriesEntity.setName(name);
        courseSeriesEntity.setUpdateTime(new Date());
        int count = courseSeriesDao.updateByPrimaryKeySelective(courseSeriesEntity);
        if (count <= 0 ){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("添加失败");
            return servicesResult;
        }
        servicesResult.setResult(true);
        return servicesResult;
    }

    @Override
    public ServicesResult<List<CourseSeriesEntity>> listByMid(Long memberId) {
        ServicesResult<List<CourseSeriesEntity>> servicesResult = new ServicesResult<>();
        if (memberId == null || memberId <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        List<CourseSeriesEntity> courseSeriesList = courseSeriesDao.selectByMid(memberId);
        servicesResult.setResult(courseSeriesList);
        return servicesResult;
    }
}
